﻿using System;
using System.Collections.Generic;
using System.Windows.Automation;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using QuickUIT.Controls;

namespace QuickUIT.Tests
{
    [TestClass]
    public class UITreeItemTests
    {
        private BaseState _baseState;

        [TestInitialize]
        public void TestSetup()
        {
            _baseState = new BaseState(TestUtilities.GetTestApplicationFileName(), "@Name='QuickUIT Test Application'");
            _baseState.Execute();
        }

        [TestMethod]
        public void ShouldGetCanExpandProperty()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITreeItem treeItem = window.UITree("@AutomationId='treeView1'").UITreeItem("@Name='Tree Item 1'");
            Assert.IsTrue(treeItem.CanExpand);
        }

        [TestMethod]
        public void ShouldGetCannotExpandOnLeafNode()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITreeItem treeItem1 = window.UITree("@AutomationId='treeView1'").UITreeItem("@Name='Tree Item 1'");
            treeItem1.Expand();
            UITreeItem treeItem2 = treeItem1.UITreeItem("@Name='Tree Sub Item 1'");
            Assert.IsFalse(treeItem2.CanExpand);
        }

        [TestMethod]
        public void ShouldGetIsExpandedProperty()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITreeItem treeItem = window.UITree("@AutomationId='treeView1'").UITreeItem("@Name='Tree Item 1'");
            Assert.IsFalse(treeItem.IsExpanded);
        }

        [TestMethod]
        public void ShouldGetIsSelectedProperty()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITreeItem treeItem = window.UITree("@AutomationId='treeView1'").UITreeItem("@Name='Tree Item 1'");
            Assert.IsFalse(treeItem.IsSelected);
        }

        [TestMethod]
        public void ShouldExpand()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITreeItem treeItem = window.UITree("@AutomationId='treeView1'").UITreeItem("@Name='Tree Item 1'");
            treeItem.Expand();
            Assert.IsTrue(treeItem.IsExpanded);
        }

        [TestMethod]
        public void ShouldCollapse()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITreeItem treeItem = window.UITree("@AutomationId='treeView1'").UITreeItem("@Name='Tree Item 1'");
            treeItem.Expand();
            Assert.IsTrue(treeItem.IsExpanded);
            treeItem.Collapse();
            Assert.IsFalse(treeItem.IsExpanded);
        }

        [TestMethod]
        public void ShouldScrollIntoView()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITreeItem treeItem = window.UITree("@AutomationId='treeView1'").UITreeItem("@Name='Tree Item 2'");
            treeItem.ScrollIntoView();
            Assert.IsFalse(treeItem.IsOffscreen);
        }

        [TestMethod]
        public void ShouldSelect()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITreeItem treeItem = window.UITree("@AutomationId='treeView1'").UITreeItem("@Name='Tree Item 2'");
            treeItem.Select();
            Assert.IsTrue(treeItem.IsSelected);
        }

        [TestMethod]
        public void ShouldAddToSelection()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITreeItem treeItem1 = window.UITree("@AutomationId='treeView1'").UITreeItem("@Name='Tree Item 1'");
            treeItem1.AddToSelection();
            Assert.IsTrue(treeItem1.IsSelected);

            UITreeItem treeItem2 = window.UITree("@AutomationId='treeView1'").UITreeItem("@Name='Tree Item 1'");
            treeItem2.AddToSelection();
            Assert.IsTrue(treeItem2.IsSelected);
        }

        [TestMethod]
        public void ShouldRemoveFromSelection()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITreeItem treeItem = window.UITree("@AutomationId='treeView1'").UITreeItem("@Name='Tree Item 1'");
            treeItem.AddToSelection();
            Assert.IsTrue(treeItem.IsSelected);

            treeItem.RemoveFromSelection();
            Assert.IsFalse(treeItem.IsSelected);
        }

        [TestMethod]
        public void ShouldGetAllTreeItems()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            List<UITreeItem> treeItems = window.UITree("@AutomationId='treeView1'").UITreeItems();
            Assert.AreEqual(2, treeItems.Count);
            UITreeItem treeItem1 = treeItems[0];
            UITreeItem treeItem2 = treeItems[1]; 
            Assert.AreEqual("Tree Item 1", treeItem1.Name);
            Assert.AreEqual("Tree Item 2", treeItem2.Name);
        }

        [TestMethod]
        public void ShouldGetContainerTree()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITreeItem treeItem = window.UITree("@AutomationId='treeView1'").UITreeItem("@Name='Tree Item 1'");
            AutomationElement container = treeItem.GetSelectionContainer();
            Assert.IsNotNull(container);
            Assert.AreEqual("treeView1", container.Current.AutomationId);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void ShouldThrowExceptionOnInvokeForUnsupportedTreeItem()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UITreeItem treeItem = window.UITree("@AutomationId='treeView1'").UITreeItem("@Name='Tree Item 1'");
            treeItem.Invoke();
        }

        [TestCleanup]
        public void TestTeardown()
        {
            _baseState.Cleanup();
        }
    }
}